//
//  DHRootNavigationView.m
//  Dresshere
//
//  Created by ZhangPeng on 2018/4/12.
//  Copyright © 2018年 张朋. All rights reserved.
//

#import "DHRootNavigationView.h"
#import "DHLeftMenuView.h"
#import "DHLeftNotLogView.h"
#import "DHS2_SearchController.h"
#import "DHS2_MessageViewController.h"

@interface DHRootNavigationView()

@property (strong, nonatomic) UIButton *menuButton; //左菜单按钮
@property (strong, nonatomic) UIButton *searchButton; //搜索按钮
@property (strong, nonatomic) UILabel *titleLabel; //标题标签

@property (strong, nonatomic) DHLeftMenuView *leftMenuview;
@property (strong, nonatomic) DHLeftNotLogView *leftNoLogMenuView;

@end

@implementation DHRootNavigationView

- (instancetype)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self) {
        [self setupViews];
        self.backgroundColor = [UIColor whiteColor];
    }
    return self;
}

- (void)setupViews{
    int spaceTopY = 20;
    int buttonW = 40;
    
    if (!self.menuButton) {
        self.menuButton = [[UIButton alloc] initWithFrame:CGRectMake(0, spaceTopY, buttonW, buttonW)];
        [self.menuButton setImage:[UIImage imageNamed:@"home_menu"] forState:UIControlStateNormal];
        [self.menuButton addTarget:self action:@selector(menuAction:) forControlEvents:UIControlEventTouchUpInside];
        [self addSubview:self.menuButton];
    }
    if (!self.searchButton) {
        self.searchButton = [[UIButton alloc] initWithFrame:CGRectMake(self.menuButton.right, spaceTopY, buttonW, buttonW)];
        [self.searchButton setImage:[UIImage imageNamed:@"home_search"] forState:UIControlStateNormal];
        [self.searchButton addTarget:self action:@selector(searchAction:) forControlEvents:UIControlEventTouchUpInside];
        [self addSubview:self.searchButton];
    }
    if (!self.titleLabel) {
        self.titleLabel = [[UILabel alloc] initWithFrame:CGRectMake(0, spaceTopY, kScreenWidth, buttonW)];
        self.titleLabel.font = [UIFont systemFontOfSize:20];
        self.titleLabel.textColor = [UIColor blackColor];
        self.titleLabel.textAlignment = NSTextAlignmentCenter;
        [self addSubview:self.titleLabel];
    }
    if (!self.messageButton) {
        self.messageButton = [[MessageButton alloc] initWithFrame:CGRectMake(kScreenWidth - buttonW, spaceTopY, buttonW, buttonW)];
        [self.messageButton setImage:[UIImage imageNamed:@"message"] forState:UIControlStateNormal];
        [self.messageButton addTarget:self action:@selector(messageAction:) forControlEvents:UIControlEventTouchUpInside];
        [self addSubview:self.messageButton];
    }
    
    UIView *lineView = [[UIView alloc] initWithFrame:CGRectMake(0, 63, kScreenWidth, 1)];
    lineView.backgroundColor = [UIColor colorWithHexString:@"#BABABA"];
    [self addSubview:lineView];
}

- (void)setTitleWithString:(NSString *)title{
    self.titleLabel.text = title;
}

- (void)setTitleWithArray:(NSArray *)titleArr{
    [self.titleLabel removeFromSuperview];
    
    self.zixunButton = [[UIButton alloc] initWithFrame:CGRectMake(kScreenWidth / 2 - 60, 20, 60, 40)];
    [self.zixunButton setTitle:titleArr[0] forState:UIControlStateNormal];
    self.zixunButton.titleLabel.font = [UIFont systemFontOfSize:20];
    [self.zixunButton setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
    
    WeakSelf(weakSelf)
    [self.zixunButton bk_addEventHandler:^(id sender) {
        [weakSelf.zixunButton routerEventWithName:@"zixun_click" userInfo:nil];
    } forControlEvents:UIControlEventTouchUpInside];
    [self addSubview:self.zixunButton];
    
    self.shequButton = [[UIButton alloc] initWithFrame:CGRectMake(kScreenWidth / 2, 20, 60, 40)];
    [self.shequButton setTitle:titleArr[1] forState:UIControlStateNormal];
    self.shequButton.titleLabel.font = [UIFont systemFontOfSize:15];
    [self.shequButton setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
    [self.shequButton bk_addEventHandler:^(id sender) {
        [weakSelf.shequButton routerEventWithName:@"shequ_click" userInfo:nil];
    } forControlEvents:UIControlEventTouchUpInside];
    [self addSubview:self.shequButton];
    
    self.editButton = [[UIButton alloc] initWithFrame:CGRectMake(self.messageButton.left - self.messageButton.width, self.messageButton.top, self.messageButton.width, self.messageButton.height)];
    [self.editButton setImage:[UIImage imageNamed:@"edit"] forState:UIControlStateNormal];
    [self addSubview:self.editButton];
    [self.editButton bk_addEventHandler:^(id sender) {
        [weakSelf routerEventWithName:@"community_publish" userInfo:nil];
    } forControlEvents:UIControlEventTouchUpInside];
    self.editButton.hidden = YES;
}

- (void)setTitleWithImage:(UIImage *)titleImage{
    [self.titleLabel removeAllSubviews];
    UIImageView *imageView = [[UIImageView alloc] initWithFrame:CGRectMake(0, 35, self.width, 15)];
    imageView.image = titleImage;
    imageView.contentMode = UIViewContentModeScaleAspectFit;
    [self addSubview:imageView];
}

- (void)menuAction:(id)sender{
    if ([Login isLogin]) {
        [self showLeftMenuView];
        self.viewController.tabBarController.tabBar.hidden = YES;
    }else{
        [self showLeftNoLogMenuView];
        self.viewController.tabBarController.tabBar.hidden = YES;
    }
}

- (void)searchAction:(id)sender{
    DHS2_SearchController *searchVC = [[DHS2_SearchController alloc] init];
    [self.viewController.navigationController pushViewController:searchVC animated:YES];
    self.viewController.tabBarController.tabBar.hidden = YES;
}

- (void)messageAction:(id)sender{
    DHS2_MessageViewController *vc = [[DHS2_MessageViewController alloc] init];
    vc.hidesBottomBarWhenPushed = YES;
    [self.viewController.navigationController pushViewController:vc animated:YES];
}

- (void)showLeftMenuView{
    if (!self.leftMenuBGView) {
        self.leftMenuBGView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, kScreenWidth, kScreenHeight)];
        self.leftMenuBGView.backgroundColor = [[UIColor blackColor] colorWithAlphaComponent:0.0];
        [self.viewController.view addSubview:self.leftMenuBGView];
    }
    if (!self.leftMenuview) {
        self.leftMenuview = [DHLeftMenuView shareDHLeftMenuView];
        [self.leftMenuBGView addSubview:self.leftMenuview];
        
        UIButton *hideButton = [[UIButton alloc] initWithFrame:CGRectMake(kScreenWidth/3 * 2, 0, kScreenWidth/3, kScreenHeight)];
        [hideButton setBackgroundColor:[UIColor clearColor]];
        [self.leftMenuBGView addSubview:hideButton];
        WeakSelf(weakSelf)
        [hideButton bk_addEventHandler:^(id sender) {
            [weakSelf hideLeftMenuView];
        } forControlEvents:UIControlEventTouchUpInside];
    }
    
    self.leftMenuBGView.hidden = NO;
    [self.leftMenuview layoutSubviews];
    [UIView animateWithDuration:0.3 animations:^{
        self.leftMenuview.left = 0;
        self.leftMenuBGView.backgroundColor = [[UIColor blackColor] colorWithAlphaComponent:0.6];
    }];
}

- (void)showLeftNoLogMenuView{
    if (!self.leftMenuBGView) {
        self.leftMenuBGView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, kScreenWidth, kScreenHeight)];
        self.leftMenuBGView.backgroundColor = [[UIColor blackColor] colorWithAlphaComponent:0.0];
        [self.viewController.view addSubview:self.leftMenuBGView];
    }
    if (!self.leftNoLogMenuView) {
        self.leftNoLogMenuView = [DHLeftNotLogView shareDHLeftNotLogView];
        [self.leftMenuBGView addSubview:self.leftNoLogMenuView];
        
        UIButton *hideButton = [[UIButton alloc] initWithFrame:CGRectMake(kScreenWidth/3 * 2, 0, kScreenWidth/3, kScreenHeight)];
        [hideButton setBackgroundColor:[UIColor clearColor]];
        [self.leftMenuBGView addSubview:hideButton];
        WeakSelf(weakSelf)
        [hideButton bk_addEventHandler:^(id sender) {
            [weakSelf hideLeftMenuView];
        } forControlEvents:UIControlEventTouchUpInside];
    }
    
    self.leftMenuBGView.hidden = NO;
    [UIView animateWithDuration:0.3 animations:^{
        self.leftNoLogMenuView.left = 0;
        self.leftMenuBGView.backgroundColor = [[UIColor blackColor] colorWithAlphaComponent:0.6];
    }];
}

- (void)hideLeftMenuView{
    [UIView animateWithDuration:0.3 animations:^{
        self.leftMenuview.left = -kScreenWidth/3 * 2;
        self.leftNoLogMenuView.left = -kScreenWidth/3 * 2;
        self.leftMenuBGView.backgroundColor = [[UIColor blackColor] colorWithAlphaComponent:0.0];
    } completion:^(BOOL finished) {
        self.leftMenuBGView.hidden = YES;
        self.viewController.tabBarController.tabBar.hidden = NO;
    }];
}

@end
